home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Games / MoofWars / Tim's Libraries / TGraphic.h < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  6.7 KB  |  195 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        TGraphic.h
  3.  
  4.     Contains:    See TGraphic.cp for description and the list of changes.
  5.  
  6.     Written by: Timothy Carroll    
  7.  
  8.     Copyright:    Copyright © 1996-1999 by Apple Computer, Inc., All Rights Reserved.
  9.  
  10.                 You may incorporate this Apple sample source code into your program(s) without
  11.                 restriction. This Apple sample source code has been provided "AS IS" and the
  12.                 responsibility for its operation is yours. You are not permitted to redistribute
  13.                 this Apple sample source code as "Apple sample source code" after having made
  14.                 changes. If you're going to re-distribute the source, we require that you make
  15.                 it clear in the source that the code was descended from Apple sample source
  16.                 code, but that you've made changes.
  17.  
  18.     Change History (most recent first):
  19.                 7/2/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  20.                 
  21.  
  22. */
  23.  
  24.  
  25. #ifndef _TGRAPHIC_
  26. #define _TGRAPHIC_
  27.  
  28. #pragma once
  29.  
  30. #include <QDOffscreen.h>
  31. #include "Error Macros.h"
  32.  
  33.  
  34. #if PRAGMA_STRUCT_ALIGN
  35. #pragma options align=power
  36. #endif
  37.  
  38. enum eDrawFlag
  39. {
  40.     kDrawGraphic = 0,
  41.     kDrawBackground = 1
  42. };
  43.  
  44. // We declare the app color table here, must be defined in the app's code
  45. extern CTabHandle gAppColorTable;
  46.  
  47. class TGraphic
  48.     {
  49.     public:
  50. /*************************************************************************************
  51. Static Creator and Reference Counting
  52.     
  53. These are the routines that handle the actual creation of the objects, along
  54. with reference counting and so on.
  55. *************************************************************************************/
  56.  
  57.     static TGraphic         *NewGraphic (SInt16 resID);
  58.  
  59.     void AddReference (void);
  60.     void DisposeReference (void);
  61.  
  62. /*************************************************************************************
  63. Locking and Unlocking
  64.     
  65. You are never required to lock a graphic -- the routines run fine either way. These 
  66. functions are provided because most of the time, all of the graphics will be allocated
  67. ahead of time, and locking them high means more memory for the app, and less handles
  68. to move around.
  69. *************************************************************************************/
  70.     OSStatus    LockGraphic (void);
  71.     OSStatus    UnlockGraphic (void);
  72.     
  73.     
  74. /*************************************************************************************
  75. Creating and destroying the graphic
  76.     
  77. CreateGraphic loads the resource and processes it to the internal graphic format.
  78. DestroyGraphic disposes of any internal data allocated for a TGraphic.
  79. *************************************************************************************/
  80.     OSStatus    CreateGraphic (void);
  81.     OSStatus    DestroyGraphic (void);
  82.  
  83.  
  84. /*************************************************************************************
  85. File Output
  86.     
  87. These routines allow convenient writing of a graphic out to a resource fork, 
  88. in either the compressed format or as a PICT resource.
  89. *************************************************************************************/
  90.  
  91.     OSStatus    WriteToGraphicResource (void);
  92.     OSStatus    WriteToPICTResource (void);
  93.     
  94. /*************************************************************************************
  95. Accessors Functions
  96.  
  97. These are inlined for speed, most other information about the sprite is private and
  98. subject to change.
  99. *************************************************************************************/
  100.  
  101.     SInt16        GetResID(void) {return fResID;}
  102.     Rect        GetBounds (void) {return fBounds;}        
  103.  
  104. /*************************************************************************************
  105. Core Functions
  106.  
  107. Here are the primary functions of TGraphic, methods to draw or hit test!
  108.     
  109. CopyImage accept a point in the local coordinate system of the destination PixMap.
  110. If the "useBackground" flag is set and a background PixMap was specified, it will draw
  111. from the background to the destination, using the graphic's data only as a mask.
  112. This allows a cheap and easy "erase" function.
  113.     
  114. HitTest accepts a point in coordinates local to the TGraphic's bounds rect, and returns
  115. true if that point hits a "drawn" portion of the graphic.
  116.     
  117. Intersect is a static function that accepts two TGraphic objects and the top left
  118. coordinate for each object.  Both points should be in the same coordinate system. It 
  119. returns true if the masks intersect at any point.
  120. *************************************************************************************/
  121.     
  122.     void            CopyImage (SInt32 top, SInt32 left, Boolean useBackground);
  123.     Boolean            HitTest (SInt32 v, SInt32 h);
  124.     static Boolean    Intersect (TGraphic *object1, TGraphic *object2,
  125.                                 SInt32 v1, SInt32 h1, SInt32 v2, SInt32 h2);
  126.     
  127.     
  128.     protected:
  129.  
  130.  
  131. /*************************************************************************************
  132. Internal Functions
  133.  
  134. We have a number of routines that can load from various resource formats, along with
  135. the actual sprite encoder.
  136.  
  137. We also have a number of routines that actually get called to do the blitting.
  138. *************************************************************************************/
  139.  
  140.     OSStatus    LoadFromGraphicResource (void);
  141.     OSStatus    LoadFromPICTResource (void);
  142.     OSStatus    LoadFromICN8Resource (void);
  143.     OSStatus    LoadFromCIconResource (void);
  144.     
  145.     OSStatus    EncodeGraphic (PixMapHandle theGraphic, Rect *encodeRect);
  146.  
  147.     void GraphicClipped (SInt32 top, SInt32 left, UInt32 clipLeft, UInt32 clipRight, UInt32 clipTop, UInt32 clipBottom);
  148.     void GraphicUnclipped (SInt32 top, SInt32 left);
  149.     void BackgroundClipped (SInt32 top, SInt32 left, UInt32 clipLeft, UInt32 clipRight, UInt32 clipTop, UInt32 clipBottom);
  150.     void BackgroundUnclipped (SInt32 top, SInt32 left);
  151.  
  152.     // There are mostly useful for dumping to a PICT file, so we don't have clipped
  153.     // versions of these, and the functionality isn't exposed to outside clients.
  154.     
  155.     void DrawMaskUnclipped (SInt32 top, SInt32 left);
  156.     void HitMaskUnclipped (SInt32 top, SInt32 left);
  157.  
  158. /*************************************************************************************
  159. Constructor/Destructor
  160.  
  161. The constructor and destructor shouldn't be called directly.  These routines are
  162. called automatically when a new sprite is created by the static routine, and when
  163. all references are released.
  164. *************************************************************************************/
  165.  
  166.     TGraphic (SInt16 resID);
  167.     ~TGraphic(void);
  168.  
  169.  
  170. /*************************************************************************************
  171. Data Structures
  172.  
  173. I force alignment to PPC, but the alignment would be the same on 68K.    
  174. *************************************************************************************/
  175.  
  176.     SInt16            fResID;
  177.     UInt16            fReferenceCount;
  178.         
  179.     UInt16            fBitDepth;
  180.     UInt16            fFlags;
  181.  
  182.     Rect            fBounds;
  183.  
  184. // This handle holds the data the TGraphic class uses to draw itself
  185.     Handle            fImage;
  186.     
  187. // This handle holds the data the TGraphic class uses for hit testing
  188.     Handle            fHitMask;    
  189. };
  190.  
  191. #if PRAGMA_STRUCT_ALIGN
  192. #pragma options align=reset
  193. #endif
  194.  
  195. #endif /* _TGRAPHIC_ */